package com.client.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.client.pojo.Comments;
import com.client.pojo.ForumLikes;
import com.client.pojo.Forums;
import com.client.pojo.Users;
import com.client.service.CommentsService;
import com.client.service.ForumLikesService;
import com.client.service.ForumsService;
import com.client.service.UsersService;
import com.client.utils.ResponseMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 帖子表 前端控制器
 * </p>
 *该类用于处理论坛发送的请求
 * @author ckick
 * @since 2025-09-20 08:28:54
 */
@RestController
public class ForumsController {
    @Autowired
    ForumsService forumsService;
    @Autowired
    UsersService usersService;
    @Autowired
    CommentsService commentsService;
    @Autowired
    ForumLikesService forumLikesService;

    /**
     * 查询论坛列表
     * @return
     */
    @RequestMapping("forumList")
    ResponseMsg forumList(){
        List<Forums> forumsList=forumsService.forumList();
        return  ResponseMsg.getInstance(200,forumsList);
    }

    /**
     * 查询论坛详情
     * @param postId
     * @return
     */
    @RequestMapping("forumById")
    ResponseMsg forumById(@RequestParam Integer postId){
        Forums forums = forumsService.getById(postId);
        Users user = usersService.getById(forums.getAuthorId());
        QueryWrapper<Comments> wrapper = new QueryWrapper<>();
        wrapper.eq("target_type","forum")
                .eq("target_id",forums.getPostId());
        List<Comments> commentsList = commentsService.list(wrapper);
        for (Comments comments:commentsList) {
            comments.setUser(usersService.getById(comments.getAuthorId()));
        }
        forums.setUser(user);
        forums.setCommentsList(commentsList);
        return ResponseMsg.getInstance(200,forums);
    }

    /**
     * 发布论坛
     * @param forums
     * @return
     */
    @RequestMapping("forumAdd")
    ResponseMsg forumAdd(@RequestBody Forums forums){
        boolean isSuccess = forumsService.save(forums);
        if (isSuccess){
            return ResponseMsg.getInstance(ResponseMsg.SUCCESS,"发布成功");
        }
        return ResponseMsg.getInstance(ResponseMsg.SUCCESS,"发布失败");
    }

    /**
     * 增加点赞
     * @param forumLikes
     * @return
     */
    @RequestMapping("forumLikesAdd")
    ResponseMsg forumLikesAdd(@RequestBody ForumLikes forumLikes){
        //查询该评论是否已被点赞
        QueryWrapper<ForumLikes> forumLikesQueryWrapper = new QueryWrapper<>();
        forumLikesQueryWrapper.eq("post_id",forumLikes.getPostId())
                .eq("user_id",forumLikes.getUserId());
        if (forumLikesService.getOne(forumLikesQueryWrapper)!=null){
           return ResponseMsg.getInstance(ResponseMsg.ERROR,"以点赞，无需重复点赞");
        }
        //修改点赞量并增加点赞记录
        UpdateWrapper<Forums> wrapper = new UpdateWrapper<>();
        wrapper.eq("post_id",forumLikes.getPostId())
                .setSql("likes = likes + 1");
        boolean isSuccess = forumsService.update(wrapper);
        boolean save = forumLikesService.save(forumLikes);
        if (isSuccess && save ){
            return ResponseMsg.getInstance(ResponseMsg.SUCCESS,"点赞成功");
        }
        return ResponseMsg.getInstance(ResponseMsg.ERROR,"点赞失败");
    }

    /**
     * 取消点赞
     * @param postId
     * @param userId
     * @return
     */
    @RequestMapping("forumLikesDel")
    ResponseMsg forumLikesDel(@RequestParam Integer postId, @RequestParam Integer userId){
        //删除点赞记录
        QueryWrapper<ForumLikes> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId)
                .eq("post_id",postId);
        boolean remove = forumLikesService.remove(wrapper);
        //修改点赞量
        UpdateWrapper<Forums> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("post_id",postId)
                .setSql("likes = likes - 1");
        boolean remove1 = forumsService.update(updateWrapper);
        if (remove1&&remove){
            ResponseMsg.getInstance(200,"取消点赞成功");
        }
        return ResponseMsg.getInstance(-200,"取消点赞失败");
    }
}
